<?php
include($_SERVER['DOCUMENT_ROOT']."/assets/inc/functions.inc.php");
include("constants.php");
dbConnect();

// array for JSON response
$response = array();

// check for get data
if (isset($_POST["gccode"])) {
    $gccode = $_POST['gccode'];

    // get a geocache from geocaches  table
    $result = mysql_query("SELECT * FROM Caches WHERE gccode = '$gccode'");
	
    if (!empty($result)) {
        // check for single result
        if (mysql_num_rows($result) == 1) {

            $result = mysql_fetch_array($result);

            $geocache = array();
            $geocache["gccode"] = $result["gccode"];
            $geocache["cache_name"] = htmlentities($result["cache_name"]);
            $geocache["cache_type"] = $result["cache_type"];
            $geocache["owner"] = htmlentities($result["owner"]);
            $geocache["difficulty"] = $result["difficulty"];
            $geocache["terrain"] = $result["terrain"];
			$geocache["date_hidden"] = $result["date_hidden"];
            
			// found a geoceache - set returncode = 1
            $response[$TAG_RETURNCODE] = $QUERY_OK;

            // usedata node
            $response[$TAG_USEDATA] = array();

            array_push($response[$TAG_USEDATA], $geocache);

            // echoing JSON response
			echo json_encode($response);
        }
		else {
            // no or more geocaches found - set returncode = 0
			$response[$TAG_RETURNCODE] = $OBJECT_NOT_FOUND;
			$response[$TAG_MESSAGE] = "Database Error. Query returned null.";

			// echo no users JSON
			echo json_encode($response);
    	}
    } 
} else {
    // required field is missing - set returncode = -1
    $response[$TAG_RETURNCODE] = $REQUIRED_FIELD_MISSING;
    $response[$TAG_MESSAGE] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
